package com.lisa.investigate;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lisa.util.UtilCommon;
import com.lisa.util.UtilKEY;
import com.lisa.util.jdbc.DBManager;
import com.lisa.util.json.JSONUtils;

/**
 * Servlet implementation class T_InsertData
 */
/*@WebServlet(description = "测试：删除原有数据，插入新数据", urlPatterns = { "/T_InsertData" })*/
/**
 * @author lisa
 * 
 */
public class T_InsertData extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private static final String RESULT_ERROR = "1";
	private static final String RESULT_SUCCESS = "0";

	private int insertCount = 0;
	private String result = RESULT_ERROR;

	/** key:表名称 */
	private String JSON_KEY_TableName = null;
	/** key:调查期 */
	private String JSON_KEY_Term = null;
	/** key:分类目录 */
	private String JSON_KEY_Catalog = null;
	/** key:调查对象 */
	private String JSON_KEY_Target = null;
	/** key:实体数据 */
	private String JSON_KEY_DATA = null;
	/**
	 * 筛选条件
	 */
	private Map<String, String> mFilters = null;
	/**
	 * 需要获取的列
	 */
	private String[] mColomns = null;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public T_InsertData() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		int countInsert = 0; 			// 插入的记录数量
		int countDelete = 0; 			// 删除的记录数量
		String strError = ""; 			// 错误信息
		Map<String, Object> allInput = null;
		
		try {
			UtilCommon.initConfigParam(this);
			initConfigParam(this);

//			String strInputJson = request.getParameter("UploadData");
			String initData = request.getParameter("UploadData");
			String strInputJson = new String(initData.getBytes("iso-8859-1"), "utf-8");
			
			if (strInputJson != null) {
				allInput = JSONUtils.fromJsonMap(strInputJson);
			}

			String tableName = (String) allInput.get(this.JSON_KEY_TableName);
			String target = (String) allInput.get(this.JSON_KEY_Target);
			String catalog = (String) allInput.get(this.JSON_KEY_Catalog);
			String term = (String) allInput.get(this.JSON_KEY_Term);
			List dataJ = (List) allInput.get(this.JSON_KEY_DATA);

			this.insertCount = dataJ.size();

			this.mFilters = new HashMap();
			this.mFilters.put(this.JSON_KEY_Target, "'" + target + "'");
			this.mFilters.put(this.JSON_KEY_Catalog, "'" + catalog + "'");
			this.mFilters.put(this.JSON_KEY_Term, "'" + term + "'");

			countDelete = DBManager.delete(tableName, this.mFilters);

			countInsert = DBManager.insert(tableName, dataJ);

			System.out.println();
		} catch (Exception e) {
			e.printStackTrace();

			strError = e.getMessage();
			System.out.print(strError);
			this.result = "1";
		}

		if ((this.insertCount > 0) && (countInsert == this.insertCount)) {
			this.result = "0";
		}

		response.getWriter().append("{\"error\":" + this.result + "}");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * 初始化uploadData的配置文件参数
	 * 
	 * @param servlet
	 */
	public void initConfigParam(HttpServlet servlet) {
		ServletConfig config = servlet.getServletConfig();
		JSON_KEY_TableName = config
				.getInitParameter(UtilKEY.JSONKEY_UPLOADR_TABLE_DATABASE);
		JSON_KEY_Term = config.getInitParameter(UtilKEY.JSONKEY_UPLOADR_TERM);
		JSON_KEY_Target = config
				.getInitParameter(UtilKEY.JSONKEY_UPLOADR_TARGET);
		JSON_KEY_Catalog = config
				.getInitParameter(UtilKEY.JSONKEY_UPLOADR_CATALOG);
		JSON_KEY_DATA = config.getInitParameter(UtilKEY.JSONKEY_UPLOADR_DATA);
	}
}